package common.aop;

import common.beans.PageResultBean;
import common.beans.ResultBean;
import common.exceptions.CheckException;
import common.exceptions.NoPermissionException;
import common.exceptions.UnloginException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


/**
 * 处理和包装异常
 *
 * @time:2019年01月01日21:10:36
 */
@Aspect
@Component
//@Order(-99)
public class ControllerAOP {
    private static final Logger logger = LoggerFactory.getLogger(ControllerAOP.class);

    @Pointcut("execution(public common.beans.ResultBean *(..))")
    public void controllerMethod1() {

    }

    @Around("controllerMethod1()")
    public Object handlerControllerMethod1(ProceedingJoinPoint pjp) {
        long startTime = System.currentTimeMillis();
        ResultBean<?> result;
        try {
            result = (ResultBean<?>) pjp.proceed();
        } catch (Throwable e) {
            result = handlerException(pjp, e);
        }

        return result;
    }

    private ResultBean<?> handlerException(ProceedingJoinPoint pjp, Throwable e) {
        ResultBean<?> result = new ResultBean();
        // 登陆异常 1
        if (e instanceof UnloginException) {
            result.setMsg("UnLogin");
            result.setCode(ResultBean.NO_LOGIN);
        }
        // 没有权限 2
        else if (e instanceof NoPermissionException) {
            result.setMsg("NO PERMISSION: " + e.getMessage());
            result.setCode(ResultBean.NO_PERMISSION);
        }
        // 已知异常 3
        else if (e instanceof CheckException) {
            result.setMsg(e.getLocalizedMessage());
            result.setCode(ResultBean.CHECK_FAIL);
        } else {
            logger.error(pjp.getSignature() + " error ", e);
            //TODO 未知的异常，应该格外注意，可以发送邮件通知等
            result.setMsg(e.toString());
            result.setCode(ResultBean.UNKNOWN_EXCEPTION);
        }

        return result;
    }


    @Pointcut("execution(public common.beans.PageResultBean *(..))")
    public void controllerMethod2() {

    }

    @Around("controllerMethod2()")
    public Object handlerControllerMethod2(ProceedingJoinPoint pjp) {
        long startTime = System.currentTimeMillis();
        PageResultBean<?> result;
        try {
            result = (PageResultBean<?>) pjp.proceed();
        } catch (Throwable e) {
            result = handlerPageException(pjp, e);
        }

        return result;
    }

    private PageResultBean<?> handlerPageException(ProceedingJoinPoint pjp, Throwable e) {
        PageResultBean<?> result = new PageResultBean();
        // 登陆异常 1
        if (e instanceof UnloginException) {
            result.setMsg("Unlogin");
            result.setCode(PageResultBean.NO_LOGIN);
        }
        // 没有权限 2
        else if (e instanceof NoPermissionException) {
            result.setMsg("NO PERMISSION: " + e.getMessage());
            result.setCode(PageResultBean.NO_PERMISSION);
        }
        // 已知异常 3
        else if (e instanceof CheckException) {
            result.setMsg(e.getLocalizedMessage());
            result.setCode(PageResultBean.CHECK_FAIL);
        } else {
            logger.error(pjp.getSignature() + " error ", e);
            //TODO 未知的异常，应该格外注意，可以发送邮件通知等
            result.setMsg(e.toString());
            result.setCode(PageResultBean.UNKNOWN_EXCEPTION);
        }

        return result;
    }
}
